#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
const int M = 100 + 10;
int maxn;
int read(){
    char ch; int sum = 0,flag = 1;
    ch = getchar();
    while(ch < '0' || ch > '9'){if(ch == '-') flag = -flag; ch = getchar();}
    while(ch >= '0' && ch <= '9'){sum = sum * 10 + ch - '0'; ch = getchar();}
    return flag * sum;
}
int T,n,m;
int opt[M][M];
int dp[200000 + 10][11][11];
int a[N],c[N],ans;
void dfs(int x){
    if(x == n + 1){
        int res = 0;
        for(int i = 1; i <= n; i++){
            for(int j = i - 1; j >= 1; j--){
                if(c[i] == c[j]){
                    if(a[i] == a[j]) res += a[i];
                    break;
                }
            }
        }
        ans = max(ans,res);
        return ;
    }
    c[x] = 1;
    dfs(x + 1);
    c[x] = 0;
    dfs(x + 1);
}
void solve1(){
	for(int i = 1; i <= n; i++){
		int res = 0;
	    for(int j = i - 1; j >= 1; j--){
			if(j < i - 1) res += (a[j] == a[j + 1]) * a[j + 1];
			for(int k = 0; k <= j - 1; k++){
				if(j == i - 1) opt[i][k + 1] = max(opt[i][k + 1],opt[i - 1][k] + (a[i] == a[j]) * a[i]);
				else opt[i][0] = max(opt[i][0],opt[j][k] + res + (a[i] == a[j]) * a[i] + (a[j + 1] == a[j - k - 1]) * a[j + 1]);
			}
		}	
	}
	for(int i = 0; i < n; i++) ans = max(ans,opt[n][i]);
	printf("%d\n",ans);
	return ; 
}
void solve3(){
    for(int i = 1; i <= n; i++){
	    for(int j = 0; j <= 10; j++){
		    dp[i][a[i]][a[i - 1]] = max(dp[i][a[i]][a[i - 1]],dp[i - 1][j][a[i - 1]] + (j == a[i]) * a[i]);
		    dp[i][a[i - 1]][a[i]] = max(dp[i][a[i - 1]][a[i]],dp[i - 1][a[i - 1]][j] + (j == a[i]) * a[i]);
		    dp[i][a[i]][j] = max(dp[i][a[i]][j],dp[i - 1][a[i - 1]][j] + (a[i] == a[i - 1]) * a[i]);
		    dp[i][j][a[i]] = max(dp[i][j][a[i]],dp[i - 1][j][a[i - 1]] + (a[i] == a[i - 1]) * a[i]);
		}	
	}
	for(int i = 0; i <= 10; i++){
	    for(int j = 0; j <= 10; j++){
		    ans = max(ans,dp[n][i][j]);	
		}	
	}
	printf("%d\n",ans);
	return ;
}
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
    T = read();
    while(T--){
	    n = read();
	    ans = 0;
	    for(int i = 1; i <= n; i++){
		    a[i] = read();	
		    maxn = max(maxn,a[i]);
		}	
		if(n <= 15) {
		    dfs(1);
		    printf("%d\n",ans);	
		}
		else if(n <= 100){
			memset(opt,0,sizeof(opt));
			ans = 0;
		    solve1();	
		}
		else if(maxn <= 10){
			ans = 0;
			memset(dp,0,sizeof(dp));
		    solve3();	
		}
	}
    return 0;
}
